// components/tabbar/tabbar.js
import utils from '../utils/util'
const url = utils.getCurrentPageUrl
Component({
  /**
   * 组件的属性列表
   */
  // properties: {
  //   imageStatus: {
  //     type: String,
  //     value: ''
  //   },
  //   mineImage: {
  //     type: String,
  //     value: ''
  //   }
  // },

  /**
   * 组件的初始数据
   */
  data: {
    isRotate: true,
    isUp: false, // 是否向上，是则可以操作回到上方
    homeimg: 'click',
    imageStatus: '/images/icon/home/home-click.png', // 方向向上则home-up，否则home-click
    mineImage: '/images/icon/home/mine.png',
    imageHome1: '/images/icon/home/home.png',
    imageHome2: '/images/icon/home/home-click.png',
    imageHome3: '/images/icon/home/home-up.png',
    imageAdd: '/images/icon/home/add.png',
    imageMine: '/images/icon/home/mine.png',
    imageMine1: '/images/icon/home/mine.png',
    imageMine2: '/images/icon/home/mine-click.png'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeImageHome(event) {
      console.log(event)
      this.setData({
        isUp: event === 'up' ? true : false,
        imageStatus: event === 'up' ? this.data.imageHome3 : this.data.imageHome2
      })
    },
    changeHomeStatus() {
      console.log(this.data.isUp)
      if(this.data.isUp) {
        wx.pageScrollTo({
          scrollTop: 1
        })
      }
      // this.setData({
      //   imageStatus: this.data.imageHome2,
      //   mineImage: this.data.imageMine1
      // })
      if('/' + url() != '/pages/index/index') {
        wx.switchTab({
          url: '/pages/index/index',
        })
        getApp().globalData.mineurl = '/images/icon/home/home-click.png'
        getApp().globalData.homeurl = '/images/icon/home/mine.png'
        getApp().globalData.tabbar = 'home'
        getApp().watch(getApp().globalData.homeThis.watchBack, getApp().globalData.homeThis)
      }
      
    },
    // addItem 新增项目
    addItem() {
      this.setData({
        isRotate: !this.data.isRotate
      })
    },
    changeRotate() {
      this.setData({
        isRotate: !this.data.isRotate
      })
    },
    // 前往我的页面
    toMinePage() {
      // this.setData({
      //   imageStatus: this.data.imageHome1,
      //   mineImage: this.data.imageMine2
      // })
      if('/' + url() !== '/pages/mine/mine') {
        wx.switchTab({
          url: '/pages/mine/mine',
        })
        getApp().globalData.mineurl = '/images/icon/home/mine-click.png'
        getApp().globalData.homeurl = '/images/icon/home/home.png'
        getApp().globalData.tabbar = 'mine'
        getApp().watch(getApp().globalData.mineThis.watchBack, getApp().globalData.mineThis)
      }
      
    },
    watchBack(value, myThis, type) {
      if(type === 'homeimg') {
        getApp().globalData.homeThis.setData({
          imageStatus: value === 'up' ? getApp().globalData.homeThis.data.imageHome3 : getApp().globalData.homeThis.data.imageHome2,
          mineImage: '/images/icon/home/mine.png'
        })
        console.log('homeimg被执行', value, myThis.data.imageStatus)
        console.log(myThis)
      } else if (type === 'tabbar') {
        console.log('tabbar被执行')
        if (value === 'home') {
          getApp().globalData.homeThis.setData({
            imageStatus: getApp().globalData.homeThis.data.isUp ? this.data.imageHome3 : this.data.imageHome2,
            mineImage: '/images/icon/home/mine.png'
          })
          if(getApp().globalData.mineThis) {
            getApp().globalData.mineThis.setData({
              imageStatus: getApp().globalData.homeThis.data.isUp ? '/images/icon/home/home-up.png' : '/images/icon/home/home-click.png',
              mineImage: '/images/icon/home/mine.png'
            })
          }
        } else {
          getApp().globalData.mineThis.setData({
            imageStatus: '/images/icon/home/home.png',
            mineImage: '/images/icon/home/mine-click.png'
          })
          getApp().globalData.homeThis.setData({
            imageStatus: '/images/icon/home/home.png',
            mineImage: '/images/icon/home/mine-click.png'
          })
          console.log('执行我的的变化')
          console.log(myThis)
        }
      }
    }
  },
  attached() {
    this.setData({
      imageStatus: getApp().globalData.homeurl,
      mineImage: getApp().globalData.mineurl
    })
    if (getApp().globalData.tabbar !== 'mine') {
      getApp().globalData.homeThis = this
      getApp().watch(getApp().globalData.homeThis.watchBack, getApp().globalData.homeThis)
    } else {
      getApp().globalData.mineThis = this
      getApp().watch(getApp().globalData.mineThis.watchBack, getApp().globalData.mineThis)
    }
  },
  created() {
  }
})
